﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;

namespace ZBJF.Warranty.BLL
{
    public partial class CellPhoneServiceProjectService
    {
        /// <summary>
        /// 获取保险产品下的手机维修服务项目
        /// </summary>
        /// <param name="insuranceProductsId">保险产品ID</param>
        /// <returns></returns>
        public List<CellPhoneServiceProjectModel> GetProductCellPhoneServiceProjectList(int insuranceProductsId)
        {
            List<CellPhoneServiceProjectModel> result = (from a in LoadEntities(t => t.InsuranceProductsID == insuranceProductsId)
                                                         join b in ServiceFactory.ServiceProjectServiceInstance.LoadEntities(t => true) on a.ServiceProjectId equals b.ID into temp
                                                         from c in temp.DefaultIfEmpty()
                                                         select new CellPhoneServiceProjectModel
                                                         {
                                                             ID = a.ID,
                                                             ServiceProjectId = a.ServiceProjectId,
                                                             ServiceProjectName=c.ProjectName,
                                                             InsuranceDevice = a.InsuranceDevice,
                                                             InsuranceDeviceType = a.InsuranceDeviceType,
                                                             NewMachineInsuranceTimeLimit = a.NewMachineInsuranceTimeLimit,
                                                             ServiceTimes = a.ServiceTimes,
                                                             UseModelTypeOption = a.UseModelTypeOption,
                                                             ModelTypeJudgeType = a.ModelTypeJudgeType,
                                                             UseScreenTypeOption = a.UseScreenTypeOption,
                                                             ScreenTypeJudgeType = a.ScreenTypeJudgeType,
                                                             UsePriceOption = a.UsePriceOption,
                                                             ProductAmountFrom = a.ProductAmountFrom,
                                                             ProductAmountTo = a.ProductAmountTo,
                                                             InsuredAmount=a.InsuredAmount
                                                         }).ToList();
                
            if (result == null || result.Count() < 1)
                return result;

            var screenSPs = ServiceFactory.InsuranceProductScreenSPsServiceInstance;   //2.1 配件等级
            var warrantyMaintenanceTypes = ServiceFactory.InsuranceProductWarrantyMaintenanceTypesServiceInstance; //2.2 添加可维修方式
            var modelType = ServiceFactory.InsuranceProductsAvailableModelTypeServiceInstance;   //2.3 添加机型承保条件
            var screenType = ServiceFactory.InsuranceProductsAvailableScreenTypeServiceInstance;  //2.4 添加屏幕类型承保条件
            var collectItemMappings = ServiceFactory.InsuranceProductsInfoCollectItemMappingsServiceInstance;  //2.5 添加信息收集配置
            var auditProjectConfigurationMappings = ServiceFactory.InsuranceProductsAuditProjectConfigurationMappingsServiceInstance;  //2.6 添加审核项目配置
            foreach (CellPhoneServiceProjectModel item in result)
            {
                //2.1 配件等级
                item.AccessoriesLevelList = screenSPs.GetAccessoriesLevelList(item.ID);
                //2.2 添加可维修方式
                item.InsuranceWarrantyMaintenanceTypeList = warrantyMaintenanceTypes.LoadEntities(t => t.CellPhoneServiceProjectID == item.ID).Select(t => t.InsuranceWarrantyMaintenanceTypeId).ToList();
                //2.3 添加机型承保条件
                item.InsuranceProductsAvailableModelTypeModel = modelType.GetInsuranceProductAvailableModel(item.ID);
                //2.4 添加屏幕类型承保条件
                item.InsuranceProductsAvailableScreenType = screenType.LoadEntities(t => t.CellPhoneServiceProjectID == item.ID).Select(t => t.ScreenTypeCode).ToList();
                //2.5 添加信息收集配置
                item.InsuranceProductsInfoCollectItemMappings = collectItemMappings.LoadEntities(t => t.CellPhoneServiceProjectID == item.ID).ToList()
                    .Select(t => new InsuranceProductsInfoCollectItemMappings
                {
                    CanUploadPicCount = t.CanUploadPicCount,
                    IsMust = t.IsMust,
                    CollectTypeCollectItemMappingID = t.CollectTypeCollectItemMappingID
                }).ToList();
                //2.6 添加审核项目配置
                item.InsuranceProductsAuditProjectConfigurationMappings = auditProjectConfigurationMappings.LoadEntities(t => t.CellPhoneServiceProjectID == item.ID).ToList()
                    .Select(t => new InsuranceProductsAuditProjectConfigurationMappings
                    {
                        IsMust = t.IsMust,
                         AuditProjectConfigurationID = t.AuditProjectConfigurationID
                    }).ToList();
            }

            return result;

        }
    }
}
